/*
 * @Descripttion: 
 * @version: 
 * @Author: ada.wu
 * @Date: 2023-11-02 20:13:01
 * @LastEditors: ada.wu
 * @LastEditTime: 2023-12-14 15:43:33
 */
import axios from 'axios';
import config from '@/config/index'
import { createPinia } from 'pinia'
import useAuthStore from '@/stores/useAuthStore';
import router from '../router'
const authStore = useAuthStore(createPinia());

axios.defaults.timeout = 6000;
// console.log(!config.mockjs_status)
// console.log('[EsAjax] config.mockjs_status = ', config.mockjs_status,import.meta.env.VITE_MOCKJS_STATUS, import.meta.env.VITE_HTTP_BASE_URL)
if (!config.mockjs_status) {
    // mockjs 不支持 baseUrl
    axios.defaults.baseURL = import.meta.env.VITE_HTTP_BASE_URL || 'http://127.0.0.1:8101';
}

axios.interceptors.request.use(option => {
    option.headers.Authorization = authStore.getToken();
    return option;
})

axios.interceptors.response.use(res => {
    if (res.data.code === 300) {
        router.push('/login');
    }
    return Promise.resolve(res);
}, error => {
    return Promise.reject(error);
})

export default axios;
